function A(){
    return new Promise((resolve, reject) =>{
        setTimeout(()=>{
            console.log('a');
            reject('a no')
            // resolve('a')
        },1000)
    })
}

function B(){
    console.log('b');
    
}
//0ms后A就返回了一个promise对象，状态为pending
//1000ms后 A返回的promise对象的状态变为resolved
// then中的回调函数才会执行
A().then((res) =>{
    B()
   },
    (err) =>{
    console.log(err);
})
// .catch(err =>{
//     console.log(err);
// })

class MyPromise {
    constructor(executor) {
        this.status = 'pending' //Promise对象的初始状态
        this.value = null //成功的结果
        this.reason = null //失败的原因
        this.onFulfilledCallbacks = [] //存放成功的回调函数
        this.onRejectedCallbacks = [] //存放失败的回调函数

        const resolve = (value)=>{
            if(this.status!== 'pending'){
                 this.value = value
                 this.status ='fulfilled'
                 this.onFulfilledCallbacks.forEach(fn => fn(value))
            }
        }
        const reject = (reason)=>{
            if(this.status!== 'pending'){
                 this.reason = reason
                 this.status ='rejected'
            }

        }
        executor(resolve, reject)   
    }

    then(onFulfilled,onRejected){
        onFulfilled = typeof onFulfilled !== 'function' ? onFulfilled : value => value//value => value 无意义的函数体
        onRejected = typeof onRejected !== 'function'? onRejected : reason => {throw reason} // 

        //返回一个新的promise对象
        let newPromise = new MyPromise((resolve,reject)=>{
            if(this.status === 'fulfilled'){
                setTimeout(() => {
                    try {
                        const result = onFulfilled(this.value) //作为异步任务,本该放到微任务队列中，但是人为打造太难，所以这里放到宏任务
                        if (result instanceof MyPromise) {
                            newPromise = result;
                        }
                        resolve(result)
                    } catch (error) {
                        reject(error)
                    }

                })
             
            }

            if(this.status === 'rejected'){
                setTimeout(() => {
                    try {
                        const result = onFulfilled(this.reason) //作为异步任务,本该放到微任务队列中，但是人为打造太难，所以这里放到宏任务
                        if (result instanceof MyPromise) {
                            newPromise = result;
                        }
                        resolve(result)
                    } catch (error) {
                        reject(error)
                    }

                })
             
            }

            if(this.status === 'pending'){
                this.onFulfilledCallbacks.push((value)=>{
                    setTimeout(() => {
                        try {
                            const result = onFulfilled(value) 
                            if (result instanceof MyPromise) {
                                newPromise = result;
                            }
                            resolve(result)
                        } catch (error) {
                            reject(error)
                        }
                        })
                })
                this.onRejectedCallbacks.push((reason)=>{
                    setTimeout(() => {
                        try {
                            const result = onRejected(this.reason) 
                            if (result instanceof MyPromise) {
                                newPromise = result;
                            }
                            resolve(result)
                        } catch (error) {
                            reject(error)
                        }
                        })
                })
            }
          

        })
        return newPromise
    }
}


let p=new MyPromise((resolve, reject) => {
    resolve('ok')
})
p.then(res=>{
    console.log(res);
})
// .catch(err=>{
//         console.log(err);
//      }
// )